let image = document.querySelector("img");
let leftBtn = document.querySelector(".left");
let rightBtn = document.querySelector(".right");
let body = document.querySelector("body");
let images = [
	"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2F3%2F59914c437c099.jpg%3Fdown&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1666676179&t=09be1752a5b3e168545127f0a967b214",
	"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2F2%2F54699bf470c9a.jpg&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1666676205&t=799c77915f8f7d1d4810c90d59260841",
	"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Flmg.jj20.com%2Fup%2Fallimg%2F811%2F012515104431%2F150125104431-4-1200.jpg&refer=http%3A%2F%2Flmg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1666676218&t=df4c32e370a1571e6e51164971fc7716",
	"https://img1.baidu.com/it/u=1877910717,1572929573&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800",
	"https://img1.baidu.com/it/u=1362715596,3738883027&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
];

let curIndex = 0;
setInterval(() => {
	setImage(curIndex);
	curIndex++;
	if (curIndex > images.length - 1) {
		curIndex = 0;
	}
}, 2000);

rightBtn.addEventListener("click", (e) => {
	curIndex++;
	if (curIndex > images.length - 1) {
		curIndex = 0;
	}
	setImage(curIndex);
});
leftBtn.addEventListener("click", (e) => {
	curIndex--;
	if (curIndex < 0) {
		curIndex = images.length - 1;
	}
	setImage(curIndex);
});

function setImage(index) {
	// images.forEach((item) => item.classList.remove("active"));
	image.src = images[index];
	console.log(images[index]);
	body.style.backgroundImage = "url(" + images[index] + ")";
}
